package com.w3coffee.web;

import org.json.JSONObject;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Util {
	public final static BASE64Encoder base64Encoder = new BASE64Encoder();
	public final static BASE64Decoder base64Decoder = new BASE64Decoder();

	public static boolean isEmpty(String str) {
		return str == null ? true : str.length() == 0;
	}

	/** 地球半径（米） */
	private final static double EARTH_RADIUS = 6378137;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 用Haversine公式计算两个经纬度坐标点之间的距离
	 * 
	 * @param lng1
	 *            第一个位置的经度
	 * @param lat1
	 *            第一个位置的纬度
	 * @param lng2
	 *            第二个位置的经度
	 * @param lat2
	 *            第二个位置的纬度
	 * @return 两个位置之间的距离，单位是米。
	 */
	public static double getDistance(double lng1, double lat1, double lng2,
			double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	private final static String jsonData = "{\"data\":{\"birth_day\":1,\"birth_month\":1,\"birth_year\":1972,\"city_code\":\"7\",\"country_code\":\"1\",\"edu\":[{\"departmentid\":11173,\"id\":24037,\"level\":4,\"schooli    d\":10946,\"year\":1989}],\"fansnum\":124,\"head\":\"http://app.qlogo.cn/mbloghead/0a7e3a54463f41a42fc0\",\"idolnum\":97,\"introduction\":\"\",\"isent\":0,\"isvip\":0,\"locati    on\":\"北京 石景山\",\"name\":\"gaobo218\",\"nick\":\"高波\",\"province_code\":\"11\",\"sex\":1,\"tag\":[{\"id\":\"1528908380181099595\",\"name\":\"写代码\"},{\"id\":\"1843126    437975152956\",\"name\":\"大叔\"},{\"id\":\"3116172981967911833\",\"name\":\"爱睡觉\"}],\"tweetnum\":88,\"verifyinfo\":\"\"},\"msg\":\"ok\",\"ret\":0}";

	private static void testJson() throws Exception {
		JSONObject jo = new JSONObject(jsonData);
		JSONObject data = (JSONObject) jo.get("data");
		System.out.println(data.getClass());
		System.out.println("id:" + data.getString("name"));
		System.out.println("name:" + data.getString("nick"));
	}

	public static void main(String[] args) throws Exception {

			StringBuffer buf = new StringBuffer("?qq?-sina?");
			int idx;
			while ((idx = buf.indexOf("?")) >= 0) {
				buf.delete(idx, idx + 1);
			}
			
			System.out.println(buf);
	}
}
